package dao;

import hibernateUtils.HibernateUtil;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import beans.BilleteBean;

public class HibernateBilleteDAO {
	private static HibernateBilleteDAO instancia;
	private static SessionFactory sf;

	private HibernateBilleteDAO() {

	}

	public static HibernateBilleteDAO getInstancia() {
		if (instancia == null) {
			sf = HibernateUtil.getSessionFactory();
			instancia = new HibernateBilleteDAO();
		}
		return instancia;
	}

	public void grabarBillete(BilleteBean billeteB) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(billeteB);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public BilleteBean buscarBillete(BilleteBean billeteb) {
		Session session = sf.openSession();
		BilleteBean billete = (BilleteBean) session.get(BilleteBean.class,
				billeteb.getIdBillete());
		session.close();
		return billete;
	}

	public ArrayList<BilleteBean> list() {
		Session session = sf.openSession();
		List<BilleteBean> billetes = new ArrayList<BilleteBean>();
		billetes = session.createQuery("from BilleteBean").list();
		session.close();
		return (ArrayList<BilleteBean>) billetes;
	}
}
